package com.example.datamanage.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.datamanage.mapper.MenuMapper;
import com.example.datamanage.mapper.RoleMenuMapper;
import com.example.datamanage.pojo.Menu;
import com.example.datamanage.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 28142
* @description 针对表【sys_menu】的数据库操作Service实现
* @createDate 2023-07-21 17:06:27
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu>
    implements MenuService{
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public List<Menu> getRoleMenu(Integer role) {
        return getMenus(menuMapper.getAllByRole(role));
    }

    @Override
    public List<Menu> getMenus(List<Menu> list) {
        // 一级菜单：pid为null
        List<Menu> pMenus = list.stream().filter(menu -> menu.getPid() == null).collect(Collectors.toList());
        for (Menu menu : pMenus) {
            // 二级菜单：pid为一级菜单的id
            List<Menu> cMenus = list.stream().filter(m -> menu.getId().equals(m.getPid())).collect(Collectors.toList());
            // 设置一级菜单的子菜单
            menu.setChildren(cMenus);
        }
        return pMenus;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateRoleMenu(Integer role, List<Integer> ids) {
        // 获取角色对应菜单的id集合，两个id集合的交集并不发生改变
        List<Menu> menuList = menuMapper.getAllByRole(role);
        List<Integer> oldIds = menuList.stream().map(Menu::getId).collect(Collectors.toList());
        // 当前端传递的菜单缺少一级菜单时，补上
        List<Integer> idsCopy = CollUtil.newArrayList(ids);
        for (Integer id : idsCopy) {
            Menu menu = getById(id);
            // 如果是二级菜单并且前端并没有传递它的一级菜单
            if (menu.getPid() != null && !ids.contains(menu.getPid())) {
                ids.add(menu.getPid());
            }
        }
        // 找出原来菜单id集合有的，但是新菜单id集合中没有的id作为要删除的记录
        List<Integer> delList = oldIds.stream().filter(id -> !ids.contains(id)).collect(Collectors.toList());
        // 找出原来菜单中没有的，但是新菜单中有的id作为要添加的id
        List<Integer> addList = ids.stream().filter(id -> !oldIds.contains(id)).collect(Collectors.toList());
        System.out.println(oldIds);
        System.out.println(ids);
        System.out.println(delList);
        System.out.println(addList);

        /* // 删除记录
        for (Integer id : delList) {
            LambdaQueryWrapper<RoleMenu> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(RoleMenu::getRoleId, role)
                    .eq(RoleMenu::getMenuId, id);
            roleMenuMapper.delete(wrapper);
        }
        // 插入数据
        for (Integer id : addList) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(role);
            roleMenu.setMenuId(id);
            roleMenuMapper.insert(roleMenu);
        } */
        return false;
    }

    // 根据传入的角色和菜单id集合生成RoleMenu集合
    /* private List<RoleMenu> generateRoleMenuList(Integer role, List<Integer> list) {
        List<RoleMenu> menus = new ArrayList<>();
        for (Integer id : list) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(role);
            roleMenu.setMenuId(id);
            menus.add(roleMenu);
            roleMenuMapper.insert(roleMenu);
        }
        return menus;
    } */
}




